# import smtplib
# import os
# from email import encoders
# from email.mime.base import MIMEBase
# from email.mime.multipart import MIMEMultipart
# from email.mime.text import MIMEText
# from config.config import Config
# from core.consts import REPORT_PATH
# from core.log import Log
#
# c = Config()
# logger = Log.get_logger()
#
#
# def send_email(report):
#     # 创建邮件对象
#     global smtp
#     msg = MIMEMultipart()
#     # 配置email头
#     msg["Subject"] = c.get_email("subject")
#     msg["From"] = c.get_email("user")
#     msg["To"] = c.get_email("receiver")
#
#     # 添加邮件正文
#     try:
#         # 添加简单的文本正文
#         body_text = "您好，这是接口自动化测试报告，请查收附件。"
#         msg.attach(MIMEText(body_text, 'plain', 'utf-8'))
#     except Exception as e:
#         logger.error(f"邮件正文创建失败: {e}")
#         raise
#
#     # 格式化email正文，同时将报告以附件的形式发送
#     try:
#         report_path = os.path.join(REPORT_PATH, report)
#         with open(report_path, "rb") as f:
#             body = f.read()
#             attach = MIMEBase('application', 'zip', filename=report)
#             # 给附件重命名，一般和源文件一样
#             attach.add_header('Content-Disposition', 'attachment', filename=('utf-8', '', report))
#             attach.set_payload(body)
#             encoders.encode_base64(attach)
#             msg.attach(attach)
#     except Exception as e:
#         logger.error(f"附件读取失败: {e}")
#         raise
#
#     try:
#         # 连接SMTP服务器，发送邮件
#         smtp = smtplib.SMTP()
#
#         # 根据端口决定是否使用SSL
#         port = c.get_email("port")
#         # 使用SSL连接
#         smtp = smtplib.SMTP_SSL(c.get_email("server"), port)
#         smtp.ehlo()  # 确保连接正常
#         smtp.login(c.get_email("user"), c.get_email("pwd"))
#         smtp.sendmail(c.get_email("user"), c.get_email("receiver"), msg.as_string())
#     except Exception as e:
#         print(f"发送失败: {e}")
#         logger.error(f"邮件发送失败，请检查邮件配置：{e}")
#         return False
#     else:
#         print("发送成功")
#         logger.info("邮件发送成功")
#         return True
#     finally:
#         try:
#             smtp.quit()
#         except Exception as quit_error:
#             print(f"Quit 时出现问题: {quit_error}")
#             logger.error(f"Quit 时出现问题: {quit_error}")
#
#

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from config.config import Config
from core.consts import REPORT_PATH
from core.log import Log

c = Config()
logger = Log.get_logger()

def send_email(report_url):
    # 创建邮件对象
    msg = MIMEMultipart()
    # 配置email头
    msg["Subject"] = c.get_email("subject")
    msg["From"] = c.get_email("user")
    msg["To"] = c.get_email("receiver")

    # 添加邮件正文，传递报告 URL 地址
    try:
        body_text = f"您好，这是接口自动化测试报告，请通过以下链接查看报告：\n\n{report_url}"
        msg.attach(MIMEText(body_text, 'plain', 'utf-8'))
    except Exception as e:
        logger.error(f"邮件正文创建失败: {e}")
        raise

    try:
        # 连接SMTP服务器，发送邮件
        smtp = smtplib.SMTP_SSL(c.get_email("server"), c.get_email("port"))
        smtp.ehlo()  # 确保连接正常
        smtp.login(c.get_email("user"), c.get_email("pwd"))
        smtp.sendmail(c.get_email("user"), c.get_email("receiver"), msg.as_string())
    except Exception as e:
        print(f"发送失败: {e}")
        logger.error(f"邮件发送失败，请检查邮件配置：{e}")
        return False
    else:
        print("发送成功")
        logger.info("邮件发送成功")
        return True
    finally:
        try:
            smtp.quit()
        except Exception as quit_error:
            print(f"Quit 时出现问题: {quit_error}")
            logger.error(f"Quit 时出现问题: {quit_error}")
